<?php

namespace app\store\controller;

use app\store\model\Activity as ActivityModel;
use think\Db;

/**
 * 领导管理
 * Class Leader
 * @package app\store\controller\store
 */
class Activity extends Controller
{
    /**
     * 领导列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new ActivityModel;
        $list = $model->getList();
        return $this->fetch('index', compact('list'));
    }

    /**
     * 报名列表
     * @param $activity_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function signList($activity_id = 0)
    {
        $list = Db::name('activity_sign')
            ->where('activity_id', $activity_id)
            ->where('wxapp_id', $this->getWxappId())
            ->select()
            ->toArray();
        return $this->fetch('signList', compact('list'));
    }

    /**
     * 添加活动
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function add()
    {
        $data = [
            ["value" => '会议', "name" => '会议'],
            ["value" => '活动', "name" => '活动'],
            ["value" => '培训', "name" => '培训'],
            ["value" => '其他', "name" => '其他'],
        ];
        $type = ['会议', '活动', '培训', '其他'];
        $model = new ActivityModel;
        if (!$this->request->isAjax()) {
            //服务队选项
            $team = Db::name('team')->where('wxapp_id', self::getWxappId())->order('team_id', 'asc')->select()->toArray();
            return $this->fetch('add', compact('team', 'data', 'type'));
        }
        // 新增记录
        if ($model->add($this->postData('article'))) {
            return $this->renderSuccess('添加成功', url('activity/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑活动
     * @param $activity_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     */
    public function edit($activity_id)
    {
        $type = ['捐款', '扶贫', '助残', '理事会'];
        // 活动详情
        $model = ActivityModel::detail($activity_id);
        if ($model) {
            $model['theme_label'] = explode(',', $model['theme_label']);
        }
        if (!$this->request->isAjax()) {
            $team = Db::name('team')->where('wxapp_id', self::getWxappId())->order('team_id', 'asc')->select()->toArray();
            return $this->fetch('edit', compact('model', 'team', 'type'));
        }
        // 新增记录
        if ($model->edit($this->postData('article'))) {
            return $this->renderSuccess('更新成功', url('activity/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除活动
     * @param $activity_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($activity_id)
    {
        // 领导详情
        $model = ActivityModel::detail($activity_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

}